﻿#include "RoadData.h"
#include "../DevInclude.h"



int CRoadData::roadDo(vector<CCPoint>* gFindArr,  vector<CCPoint>* ret){
    m_nDirection = 0;
    CCPoint targetPoint;
     
  	for(uint32 i=0;i<gFindArr->size();i++){
        CCPoint pos=(*gFindArr)[i];
         
        
        targetPoint =parseMapPos(pos.x,pos.y);
        if(checkFun(targetPoint.x,targetPoint.y) || i == gFindArr->size() -1){
            ret->push_back(pos);
        } 
    }
    
    return ret->size();
}

/** 使用另一种方式检查方向 **/
bool CRoadData::checkFun(float nX, float nY){
    if(int(nX - m_tOldPos.x ) != int(m_nOldDx) || int(nY - m_tOldPos.y) != int(m_nOldDy)){
        m_nOldDx = int(nX - m_tOldPos.x );
        m_nOldDy = int(nY - m_tOldPos.y);
        m_tOldPos.x = nX;
        m_tOldPos.y = nY;
        return true;
    }
    m_nOldDx = int(nX - m_tOldPos.x );
    m_nOldDy = int(nY - m_tOldPos.y);
    m_tOldPos.x = nX;
    m_tOldPos.y = nY;
    return false;
}

//根据目标转角度
uint32  CRoadData::trunAngle(float nX, float nY){
    float angle = atan2((nX - m_tOldPos.x ),(nY*2 - m_tOldPos.y*2)) * float(180/PI);
    angle = angle+180;
    //trace("角度:",angle );
    
    uint32 direction;
    //执行角色方向
    if(angle>22.5 && angle<=67.5){
        //trace("左上");
        direction = 5;
    }else if(angle>67.5 && angle<=112.5){
        //trace("上");
        direction = 6;
    }else if(angle>112.5 && angle<=157.5){
        //trace("右上");
        direction = 7;
    }else if(angle>157.5 && angle<=202.5){
        //trace("右");
        direction = 0;
    }else if(angle>202.5 && angle<=247.5){
        //trace("右下");
        direction = 1;
    }else if(angle>247.5 && angle<=292.5){
        //trace("下");
        direction = 2;
    }else if(angle>292.5 && angle<=337.5){
        //trace("左下");
        direction = 3;
    }else if(angle>337.5 || angle<=22.5){
        //trace("左");
        direction = 4;
    } else{
        direction = 0;
    }
    m_tOldPos.x = nX;
    m_tOldPos.y = nY;
    return direction;
}

